package controller;

import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Enumeration;

import javax.swing.DefaultListModel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import org.jdom2.Element;

import components.Component;

import controller.listener.lexicon.LexNewEntryButtonListener;

import features.Feature;

import lexicon.LexiconList;
import managers.ComponentManager;
import managers.LexiconManager;
import managers.XMLManager;

import rulebase.BasicRulePanel;
import rulebase.FeaturesWindow;
import rulebase.FormSelectionContainer;
import rulebase.FormSelectionPanel;
import rulebase.MorphophoneticPrefixSpelloutRulePanel;
import rulebase.MorphophoneticSpelloutContainer;
import rulebase.MorphophoneticSpelloutRulePanel;
import rulebase.MorphophoneticSuffixSpelloutRulePanel;
import rulebase.RuleContainer;
import rulebase.RuleManagementWindow;
import rulebase.SimpleSpelloutContainer;
import rulebase.SimpleSpelloutRulePanel;
import rulebase.SpelloutRulePanel;
import rulebase.TableSpelloutRulePanel;
import rulebase.TreeComponent;
import rulebase.TriggerWordFrame;
import rules.Action;
import rules.RuleInputLeaf;
import rules.RuleInputPhrase;
import view.lexicon.AddEditFeatureFrame;
import view.lexicon.LexiconPanel;

public class AddEditRuleController 
{
	private RuleManagementWindow RMW;
	private FeaturesWindow FW;
	private BasicRulePanel BRP;
	private FormSelectionPanel FSP;
	private MorphophoneticPrefixSpelloutRulePanel MPSRP;
	private MorphophoneticSuffixSpelloutRulePanel MSSRP;
	private SimpleSpelloutRulePanel SSRP;
	private SpelloutRulePanel SRP;
	private TableSpelloutRulePanel TSRP;
	private MorphophoneticSpelloutRulePanel MSRP;
	private TriggerWordFrame TWF;
	
	private JPanel pnlThree;
	private JPanel pnlSix;
	private JPanel pnlTen;
	
	private DefaultMutableTreeNode selectedInputNode;
	private DefaultMutableTreeNode selectedOutputNode;
	
	private RuleContainer RC;
	
	private TreePath lastInputNodePath;
	private TreePath lastOutputNodePath;


	public AddEditRuleController()
	{
		RMW = new RuleManagementWindow();
		
		BRP = (BasicRulePanel) RMW.getPnlFour();
		SRP = (SpelloutRulePanel) RMW.getPnlFive();
		SSRP = (SimpleSpelloutRulePanel) SRP.getPnlSeven();
		TSRP = (TableSpelloutRulePanel) SRP.getPnlEight();
		MSRP = (MorphophoneticSpelloutRulePanel) SRP.getPnlNine();
		FSP = (FormSelectionPanel) SRP.getPnlThirteen();
		
		MPSRP = (MorphophoneticPrefixSpelloutRulePanel) MSRP.getPnlEleven();
		MSSRP = (MorphophoneticSuffixSpelloutRulePanel) MSRP.getPnlTwelve();
		
		pnlThree = RMW.getPnlThree();
		pnlSix = SRP.getPnlSix();
		pnlTen = MSRP.getPnlTen();
		
		SRP.setCbxTypeActionListener(new CbxTypeListener());
		
		MSRP.setCbxMorphophoneticModificationListener(new CbxMorphophoneticModificationListener());
		
		BRP.setRdnInputListener(new RdnInputListener());
		BRP.setRdnOutputListener(new RdnOutputListener());
		BRP.setInputTreeListener(new InputTreeListener());
		BRP.setOutputTreeListener(new OutputTreeListener());
		
		
		RMW.setConstituentListener(new ClauseListener());
		RMW.setSubconstituentListener(new SubclListener());
		RMW.setNounPhraseListener(new NounPhraseListener());
		RMW.setVerbPhraseListener(new VerbPhraseListener());
		RMW.setAdjectivePhraseListener(new AdjectivePhraseListener());
		RMW.setAdverbPhraseListener(new AdverbPhraseListener());
		
		RMW.setFeaturesListener(new FeaturesListener());
		RMW.setCbxRuleListener(new CbxRuleListener());
		RMW.setSaveListener(new SaveListener());
		
		SSRP.setBtnSimpleTriggerWordListener(new triggerWordListener(SSRP.getClass().getSimpleName()));
		TSRP.setTableTriggerWordListener(new triggerWordListener(TSRP.getClass().getSimpleName()));
	}
	
	public RuleContainer getRC() 
	{
		return RC;
	}

	public void setRC(RuleContainer rC) 
	{
		RC = rC;
	}
	
	public class CbxRuleListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) 
		{
			String s = RMW.getCbxRuleSelectedItem();
			CardLayout cl = (CardLayout) pnlThree.getLayout();
			cl.show(pnlThree, s);
		}	
	}
	
	public class CbxTypeListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			String s = SRP.getCbxTypeSelectedItem();
			CardLayout cl = (CardLayout) pnlSix.getLayout();
			cl.show(pnlSix, s);
		}
	}
	
	public class CbxMorphophoneticModificationListener implements ActionListener
	{

		public void actionPerformed(ActionEvent e) 
		{
			String s = MSRP.getCbxMorphophoneticModification();
			CardLayout cl = (CardLayout) pnlTen.getLayout();
			cl.show(pnlTen, s);
		}
	}
	
	public class FeaturesListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			FW = new FeaturesWindow();
			
			FW.setCbxFeatureTypeListener(new CbxFeatureTypeListener());
			FW.setCbxPOSListener(new CbxPOSListener());
			FW.setCbxFeatureListener(new CbxFeatureListener());
			FW.setBtnAddEditFeatureListener(new BtnAddEditFeatureListener());
			FW.setFeatureJListListener(new FeatureJListListener());
			FW.setDeleteFeatureJListListener(new DeleteFeatureJListListener());
			FW.setBtnOkListener(new BtnOkListener());
		}
	}

	public class RdnInputListener implements ItemListener
	{
		public void itemStateChanged(ItemEvent e) 
		{
			if(BRP.getRdnInput().isSelected())
			{
				BRP.getRdnOutput().setSelected(false);
				
				BRP.setOutputTreeModel(new DefaultTreeModel(new DefaultMutableTreeNode("root")));
				//DefaultMutableTreeNode rootOutput = new DefaultMutableTreeNode("Output");
				//BRP.setOutputTreeModel(new DefaultTreeModel(rootOutput));
			}
		}
	}
	public void cloneTree(DefaultMutableTreeNode newChild, DefaultMutableTreeNode parent, DefaultTreeModel treeModel){
		DefaultMutableTreeNode temp = (DefaultMutableTreeNode)newChild.clone();
		treeModel.insertNodeInto(temp, parent, parent.getChildCount());
		for (Enumeration en = newChild.children(); en.hasMoreElements();)
		       cloneTree((DefaultMutableTreeNode) en.nextElement(), temp, treeModel);
	}
	
	public class RdnOutputListener implements ItemListener
	{
		public void itemStateChanged(ItemEvent e)
		{
			if(BRP.getRdnOutput().isSelected())
			{
				BRP.getRdnInput().setSelected(false);
				
			    DefaultMutableTreeNode inputRoot = (DefaultMutableTreeNode) BRP.getInputTreeModel().getRoot();
			    
			    DefaultTreeModel outputModel = BRP.getOutputTreeModel();
				DefaultMutableTreeNode outputRoot = (DefaultMutableTreeNode) outputModel.getRoot();
				
				System.out.println("A Input Root's Children: " + inputRoot.getChildCount());
				
				// copy from input to output if there is nothing on the output beforehand
				if(outputRoot.getChildCount() == 0){
					//BRP.setOutputTreeModel(inputModelCopy);
					
					for (Enumeration en = inputRoot.children(); en.hasMoreElements();)
						cloneTree((DefaultMutableTreeNode) en.nextElement(), outputRoot, outputModel);
					
				}
				BRP.setOutputTreeModel(outputModel);
				
				System.out.println("B Input Root's Children: " + inputRoot.getChildCount());
				System.out.println("C Input Root's Children: " + outputRoot.getChildCount());
			}
		}
	}

	public class ClauseListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new TreeComponent("Clause"));
			
			if(BRP.getRdnInput().isSelected())
			{
				DefaultTreeModel treeModel = BRP.getInputTreeModel();
				DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)treeModel.getRoot();
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				//inputModelCopy.insertNodeInto((DefaultMutableTreeNode)newNode.clone(),(DefaultMutableTreeNode) inputModelCopy.getRoot(), ((DefaultMutableTreeNode)inputModelCopy.getRoot()).getChildCount());
				BRP.setInputTreeModel(treeModel);
				
				System.out.println("TIOMFSKSA" + ((DefaultMutableTreeNode)BRP.getInputTreeModel().getRoot()).getChildCount());
			}
			else
			{
				DefaultTreeModel treeModel = BRP.getOutputTreeModel();
				DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)treeModel.getRoot();
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				BRP.setOutputTreeModel(treeModel);
			}
			reselect();
		}
	}
	
	public class SubclListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new TreeComponent("Subclause"));
			
			if(BRP.getRdnInput().isSelected())
			{
				DefaultTreeModel treeModel = BRP.getInputTreeModel();
				DefaultMutableTreeNode parentNode = selectedInputNode;
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());

			}
			else
			{
				DefaultTreeModel treeModel = BRP.getOutputTreeModel();
				DefaultMutableTreeNode parentNode = selectedOutputNode;
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				BRP.setOutputTreeModel(treeModel);
			}
			File fp = new File("Rules\\out.xml");
			fp.delete();
			reselect();
		}
	}
	
	public class NounPhraseListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new TreeComponent("Noun Phrase"));
			
			if(BRP.getRdnInput().isSelected())
			{
				DefaultTreeModel treeModel = BRP.getInputTreeModel();
				DefaultMutableTreeNode parentNode = selectedInputNode;
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
			}
			else
			{
				DefaultTreeModel treeModel = BRP.getOutputTreeModel();
				DefaultMutableTreeNode parentNode = selectedOutputNode;
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				BRP.setOutputTreeModel(treeModel);
			}
			reselect();
		}
	}
	
	public class VerbPhraseListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new TreeComponent("Verb Phrase"));
			
			if(BRP.getRdnInput().isSelected())
			{
				DefaultTreeModel treeModel = BRP.getInputTreeModel();
				DefaultMutableTreeNode parentNode = selectedInputNode;
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				BRP.setInputTreeModel(treeModel);

				BRP.getInputTree().setSelectionPath(BRP.getInputTree().getSelectionPath());
			}
			else
			{
				DefaultTreeModel treeModel = BRP.getOutputTreeModel();
				DefaultMutableTreeNode parentNode = selectedOutputNode;
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				BRP.setOutputTreeModel(treeModel);
			}
			reselect();
		}
	}
	
	public class AdjectivePhraseListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new TreeComponent("Adjective Phrase"));
			
			if(BRP.getRdnInput().isSelected())
			{
				DefaultTreeModel treeModel = BRP.getInputTreeModel();
				DefaultMutableTreeNode parentNode = selectedInputNode;
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				BRP.setInputTreeModel(treeModel);
			}
			else
			{
				DefaultTreeModel treeModel = BRP.getOutputTreeModel();
				DefaultMutableTreeNode parentNode = selectedOutputNode;
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				BRP.setOutputTreeModel(treeModel);
			}
			reselect();
		}
		
	}
	
	public class AdverbPhraseListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e) 
		{
			DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new TreeComponent("Adverb Phrase"));
			
			if(BRP.getRdnInput().isSelected())
			{
				DefaultTreeModel treeModel = BRP.getInputTreeModel();
				DefaultMutableTreeNode parentNode = selectedInputNode;
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				BRP.setInputTreeModel(treeModel);
			}
			else
			{
				DefaultTreeModel treeModel = BRP.getOutputTreeModel();
				DefaultMutableTreeNode parentNode = selectedOutputNode;
				treeModel.insertNodeInto(newNode, parentNode, parentNode.getChildCount());
				BRP.setOutputTreeModel(treeModel);
			}
			reselect();
		}
	}
	
	public void reselect(){
		JTree inputTree = BRP.getInputTree();
		JTree outputTree = BRP.getOutputTree();
		
		if(BRP.getRdnInput().isSelected()){
			inputTree.setSelectionPath(lastInputNodePath);
			outputTree.setSelectionPath(null);
		}
		else{	
			inputTree.setSelectionPath(null);
			outputTree.setSelectionPath(lastOutputNodePath);
		}	
	}
	
	public class InputTreeListener implements TreeSelectionListener
	{
		public void valueChanged(TreeSelectionEvent arg0) 
		{
			JTree tree = BRP.getInputTree();
			
			selectedInputNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
			lastInputNodePath = tree.getSelectionPath();
		}
	}
	
	public class OutputTreeListener implements TreeSelectionListener
	{
		public void valueChanged(TreeSelectionEvent arg0) 
		{
			JTree tree = BRP.getOutputTree();
			
			selectedOutputNode = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();
			lastOutputNodePath = tree.getSelectionPath();
			
		}
	}
	
	public class CbxFeatureTypeListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			
			FW.initCbxPOSItems();
		}
		
	}
	
	public class CbxPOSListener implements ActionListener{
		
		public void actionPerformed(ActionEvent arg0){
			
			FW.setFeatureList();
		}
	}
	
	public class CbxFeatureListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			
			FW.setFeatureJList();
		}
		
	}
	
	public class BtnAddEditFeatureListener implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			LexiconPanel lexiconPanel = new LexiconPanel();
			LexiconList lexList =  LexiconManager.getInstance().getLexiconList(lexiconPanel.getCodeFromSelectedPOS());
			new AddEditFeatureFrame(lexList); 
		}
		
	}
	
	public class FeatureJListListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent arg0) {
			FW.addSelectedFeature();			
		}

		@Override
		public void mouseEntered(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		
	}
	
	public class DeleteFeatureJListListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			FW.deleteSelectedFeature();
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) 
		{
			// TODO Auto-generated method stub
			
		}
		
	}

	public class BtnOkListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) 
		{
			DefaultListModel model = FW.getSelectedFeatureModel();
			
			//TreeComponent temp = (TreeComponent)selectedInputNode.getUserObject();
			if(SRP.getCbxTypeSelectedItem().equals("Simple"))
				SSRP.setSimpleFeaturesListModel(model);
			else if(SRP.getCbxTypeSelectedItem().equals("Form Selection"))
					FSP.setSimpleFeaturesListModel(model);
		
			FW.dispose();
		}
		
	}

		
	/*
		rule.setAttribute("name",name);
		rule.setAttribute("category",category);
		Element input = new Element("matching-criteria");
		if(matchingCriteria != null && matchingCriteria.size() != 0)
			for(Component component : matchingCriteria)
				if(ComponentManager.getInstance().isLeaf(component.getName()))
					input.addContent(((RuleInputLeaf)component).generateXMLElement());
				else
					input.addContent(((RuleInputPhrase)component).generateXMLElement());
		rule.addContent(input);
		
		Element output = new Element("output-actions");
		if(outputActions != null && outputActions.size() != 0)
			for(Action action : outputActions)
				output.addContent(action.getXMLElement());
		rule.addContent(output);

		System.out.println("ahbksdfk" +rule.getAttributeValue("name"));
	*/	
	
	
	public class SaveListener implements ActionListener
	{
		public void actionPerformed(ActionEvent arg0) 
		{
			Element xmlElement = new Element("rule");
			
			RC = new RuleContainer();
			
			RC.setRuleName(RMW.getRuleNameText());
			RC.setSyntacticCategory(RMW.getCbxSyntacticCategoryItem());
			RC.setGroup(RMW.getGroupText());

			xmlElement.setAttribute("name", RC.getRuleName());
			xmlElement.setAttribute("syntactic-category", RC.getSyntacticCategory());
			xmlElement.setAttribute("group", RC.getGroup());
			
			String Rule = RMW.getCbxRuleSelectedItem();
			
			RC.setRule(Rule);
			
			if(Rule.equalsIgnoreCase("BasicRule"))
			{
				
			}
			else
			{
				String Type = SRP.getCbxTypeSelectedItem();
				RC.setRuleType(Type);
				
				xmlElement.setAttribute("type", RC.getRuleType());
				
				if(Type.equalsIgnoreCase("Simple"))
				{
					Element matchingCriteria = new Element("matching-criteria");
					
					SimpleSpelloutContainer SimpleContainer = new SimpleSpelloutContainer();
					SimpleContainer.setBaseForm(SSRP.getCbxBaseFormText());
					
					matchingCriteria.setAttribute("base-form", "Melissa");
					
					String simpleModification = SSRP.getCbxModificationText();
					SimpleContainer.setModification(simpleModification);
					
					matchingCriteria.setAttribute("modification", SimpleContainer.getModification());
					
					
					Element outputActions = new Element("output-action");
					
					Element action;
					
					if(simpleModification.equalsIgnoreCase("Prefix")||simpleModification.equalsIgnoreCase("Circumfix"))
					{
						SimpleContainer.setPrefix(SSRP.getSimplePrefixText());
						action = new Element("action");
						action.setAttribute("type","add-prefix");
						action.setAttribute("prefix",SimpleContainer.getPrefix());
						outputActions.addContent(action);
					}
					
					if(simpleModification.equalsIgnoreCase("Suffix")||simpleModification.equalsIgnoreCase("Circumfix"))
					{
						SimpleContainer.setSuffix(SSRP.getSimpleSuffixText());
						action = new Element("action");
						action.setAttribute("type","add-suffix");
						action.setAttribute("suffix",SimpleContainer.getSuffix());
						outputActions.addContent(action);
					}
					
					if(simpleModification.equalsIgnoreCase("Infix"))
					{
						SimpleContainer.setInfix(SSRP.getSimpleInfixText());
						action = new Element("action");
						action.setAttribute("type","add-infix");
						action.setAttribute("infix",SimpleContainer.getInfix());
						outputActions.addContent(action);
					}
					
					if(simpleModification.equalsIgnoreCase("New Translation"))
					{
						SimpleContainer.setTranslation(SSRP.getSimpleTranslationText());
						action = new Element("action");
						action.setAttribute("type","add-translation");
						action.setAttribute("translation",SimpleContainer.getTranslation());
						outputActions.addContent(action);
					}
					
					if(simpleModification.equalsIgnoreCase("Add Word"))
					{
						SimpleContainer.setNewWord(SSRP.getSimpleNewWordText());
						action = new Element("action");
						action.setAttribute("type","add-word");
						action.setAttribute("word",SimpleContainer.getNewWord());
						outputActions.addContent(action);
					}
					
					SimpleContainer.setGloss(SSRP.getSimpleGlossText());
					outputActions.setAttribute("gloss", SimpleContainer.getGloss());
					
					RC.setSimpleContainer(SimpleContainer);

					xmlElement.addContent(matchingCriteria);
					xmlElement.addContent(outputActions);
				}
				else if(Type.equalsIgnoreCase("Table"))
				{
					
				}
				else if(Type.equalsIgnoreCase("Morphophonetic"))
				{
					Element matchingCriteria = new Element("actions");
					
					MorphophoneticSpelloutContainer MorphophoneticContainer = new MorphophoneticSpelloutContainer();
					MorphophoneticContainer.setModification(MSRP.getCbxMorphophoneticModification());
					
					matchingCriteria.setAttribute("modification",MorphophoneticContainer.getModification());
					
					if(MSRP.getCbxMorphophoneticModification().equalsIgnoreCase("Prefix"))
					{
						MorphophoneticContainer.setAffix(MPSRP.getTxtMorphophoneticPrefix());
						MorphophoneticContainer.setNewAffix(MPSRP.getTxtMorphophoneticNewPrefix());
						MorphophoneticContainer.setInfix(MPSRP.getTxtMorphophoneticPreInfix());
						MorphophoneticContainer.setNewInfix(MPSRP.getTxtMorphophoneticPreNewInfix());
						MorphophoneticContainer.setStem(MPSRP.getTxtMorphophoneticPreStem());
						MorphophoneticContainer.setNewStem(MPSRP.getTxtMorphophoneticPreNewStem());
								
						
						
						MorphophoneticContainer.setAffixChange(MPSRP.getCbxPrefix());
						MorphophoneticContainer.setInfixChange(MPSRP.getCbxInfix());
						MorphophoneticContainer.setStemChange(MPSRP.getCbxStem());
						

						Element criterion ;
						criterion = new Element("criterion");
						criterion.setAttribute("change",MorphophoneticContainer.getNewAffix());
						matchingCriteria.addContent(criterion);

						criterion = new Element("criterion");
						criterion.setAttribute("affix",MorphophoneticContainer.getAffix());
						matchingCriteria.addContent(criterion);			
						
						criterion = new Element("criterion");
						criterion.setAttribute("new-affix",MorphophoneticContainer.getNewAffix());
						matchingCriteria.addContent(criterion);
					}
					else
					{
						MorphophoneticContainer.setAffix(MSSRP.getTxtMorphophoneticSuffix());
						MorphophoneticContainer.setNewAffix(MSSRP.getTxtMorphophoneticNewSuffix());
						MorphophoneticContainer.setInfix(MSSRP.getTxtMorphophoneticSufInfix());
						MorphophoneticContainer.setNewInfix(MSSRP.getTxtMorphophoneticSufNewInfix());
						MorphophoneticContainer.setStem(MSSRP.getTxtMorphophoneticSufStem());
						MorphophoneticContainer.setNewStem(MSSRP.getTxtMorphophoneticSufNewStem());
						
						MorphophoneticContainer.setAffixChange(MSSRP.getCbxSuffix());
						MorphophoneticContainer.setInfixChange(MSSRP.getCbxInfix());
						MorphophoneticContainer.setStemChange(MSSRP.getCbxStem());
						
						Element criterion ;
						criterion = new Element("action");
						criterion.setAttribute("change",MorphophoneticContainer.getNewAffix());
						criterion.setAttribute("affix",MorphophoneticContainer.getAffix());
						criterion.setAttribute("new-affix",MorphophoneticContainer.getNewAffix());
						
						matchingCriteria.addContent(criterion);
					
					}
					
					xmlElement.addContent(matchingCriteria);
					
					
				}
				else if(Type.equalsIgnoreCase("Form Selection"))
				{
					FormSelectionContainer FormContainer = new FormSelectionContainer();
					FormContainer.setBaseForm(FSP.getCbxFSBaseFormText());
				}
			}

			XMLManager.getInstance().writeToXML("Rules\\"+RC.getRuleName()+".xml", xmlElement);
		}
		
	}
	
	public class triggerWordListener implements ActionListener{

		private String className ="";
		
		public triggerWordListener(String className){
			this.className = className;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			TWF = new TriggerWordFrame();
			
			TWF.setAddNewWordListener(new LexNewEntryButtonListener(TWF));
			TWF.setOkListener(new setOKListener(className));
			TWF.setCancelListener(new CancelListener());
		}
		
	}
	
	public class setOKListener implements ActionListener {
		private String className ="";
		
		public setOKListener(String className){
			this.className = className;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			if(className.equals("SimpleSpelloutRulePanel"))
				SSRP.addTriggerWord(TWF.getSelectedWord());
			else if(className.equals("TableSpelloutRulePanel"))
				TSRP.addTriggerWord(TWF.getSelectedWord());
			
			TWF.dispose();
		}
		
	}
	
	public class CancelListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			TWF.dispose();
		}
		
	}
}